from flask_sqlalchemy import SQLAlchemy
from redis import StrictRedis
from flask_session import Session
from config import config_dict
from flask_wtf import CSRFProtect
from flask import Flask


# 定义redis_store
redis_store = None

def create_app(config_name):

    config = config_dict.get(config_name)
    # 创建flask应用程序实例
    # 需要传入__name__,作用是确定资源所在路径
    app = Flask(__name__)

    app.config.from_object(config)

    # 创建SQLAlchemy
    db = SQLAlchemy(app)

    # 创建redis对象
    global redis_store
    redis_store = StrictRedis(host=config.REDIS_HOST, port=config.REDIS_HOST, decode_responses=True)

    # 读取app中session中的配置信息
    Session(app)

    # 保护app
    CSRFProtect(app)

    # 注册首页蓝图index_biue到app
    from info.modules.index import index_blue
    app.register_blueprint(index_blue)
    return app
